using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Rensselaer_Event_Finder
{
    public partial class EventsResultsMDI : Form
    {
        
        #region Public Methods

        public EventsResultsMDI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Given a location, creates controls to display basic information for the event and adds
        /// the controls to the form
        /// </summary>
        /// <param name="loc">Location to add</param>
        public void Add_Event(Event evt)
        {
            //create all controls to add for this location
            System.Windows.Forms.Panel newPanel;
            System.Windows.Forms.LinkLabel newEventName;
            System.Windows.Forms.LinkLabel newVenueName;
            System.Windows.Forms.PictureBox newPictureBox;
            System.Windows.Forms.Label newEventCategory;
            System.Windows.Forms.Label newLabel1;

            newPanel = new System.Windows.Forms.Panel();
            newLabel1 = new System.Windows.Forms.Label();
            newEventCategory = new System.Windows.Forms.Label();
            newEventName = new System.Windows.Forms.LinkLabel();
            newVenueName = new System.Windows.Forms.LinkLabel();
            newPictureBox = new System.Windows.Forms.PictureBox(); 
            newPanel.SuspendLayout();           
            ((System.ComponentModel.ISupportInitialize)(newPictureBox)).BeginInit();
            this.SuspendLayout();

            // 
            // Event Type Label
            // 
            newLabel1.AutoSize = true;
            newLabel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            newLabel1.Location = new System.Drawing.Point(109, 55);
            newLabel1.Name = evt.Name + "label1";
            newLabel1.Size = new System.Drawing.Size(79, 16);
            newLabel1.TabIndex = 4;
            newLabel1.Text = "Event type:";
            newLabel1.BackColor = Color.Black;
            newLabel1.ForeColor = Color.White;

            // 
            // EventCategory
            // 
            newEventCategory.AutoSize = true;
            newEventCategory.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            newEventCategory.Location = new System.Drawing.Point(194, 55);
            newEventCategory.Name = evt.Name + "EventCategory";
            newEventCategory.Size = new System.Drawing.Size(101, 16);
            newEventCategory.TabIndex = 2;
            newEventCategory.BackColor = Color.Black;
            newEventCategory.ForeColor = Color.White;

            //Build a string containing all categories for this Event
            if (evt.Categories.Count > 0)
            {
                newEventCategory.Text = evt.Categories[0].Name;
                for (int i = 1; i < evt.Categories.Count; i++)
                {
                    newEventCategory.Text += ", " + evt.Categories[i].Name;
                }
            }
            else
            {
                newEventCategory.Text = "N/A";
            }

            // 
            // EventNameLink
            // 
            newEventName.AutoSize = true;
            newEventName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            newEventName.LinkColor = System.Drawing.Color.Black;
            newEventName.Location = new System.Drawing.Point(108, 3);
            newEventName.Name = evt.Name + "EventNameLink";
            newEventName.Size = new System.Drawing.Size(112, 20);
            newEventName.TabIndex = 1;
            newEventName.TabStop = true;
            //add Event name to the text, set the tag as the id, and add the click event handler
            //that loads the Event based on the id stored in the tag
            newEventName.Text = evt.Name;
            newEventName.Tag = evt.Id;
            newEventName.BackColor = Color.Black;
            newEventName.ForeColor = Color.White;
            newEventName.LinkColor = Color.White;
            newEventName.Click += new EventHandler(EventName_Click);

            // 
            // VenueNameLink
            // 
            newVenueName.AutoSize = true;
            newVenueName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            newVenueName.LinkColor = System.Drawing.Color.Black;
            newVenueName.Location = new System.Drawing.Point(108, 28);
            newVenueName.Name = evt.Name + "VenueNameLink";
            newVenueName.Size = new System.Drawing.Size(112, 20);
            newVenueName.TabIndex = 2;
            newVenueName.TabStop = true;
            //add Event name to the text, set the tag as the id, and add the click event handler
            //that loads the Event based on the id stored in the tag
            newVenueName.Text = evt.Location.Name;
            newVenueName.Tag = evt.Location.Id;
            newVenueName.BackColor = Color.Black;
            newVenueName.ForeColor = Color.White;
            newVenueName.LinkColor = Color.White;
            newVenueName.Click += new EventHandler(VenueName_Click);

            // 
            // pictureBox1
            // 
            newPictureBox.Location = new System.Drawing.Point(3, 3);
            newPictureBox.Name = evt.Name + "pictureBox";
            newPictureBox.Size = new System.Drawing.Size(100, 77);
            newPictureBox.TabIndex = 0;
            newPictureBox.TabStop = false;
            newPictureBox.BackColor = Color.Black;
            newPictureBox.ForeColor = Color.White;
            newPictureBox.BorderStyle = BorderStyle.Fixed3D;
            newPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            if (evt.Image_url != "" && evt.Image_url != null)
            {
                newPictureBox.Image = Globals.loadImageFromURL(evt.Image_url);
            }
            else
            {
                newPictureBox.Image = Globals.loadImageFromURL(evt.Location.Image_url);
            }

            // 
            // Event panel
            // 
            newPanel.Controls.Add(newLabel1);
            newPanel.Controls.Add(newEventCategory);
            newPanel.Controls.Add(newEventName);
            newPanel.Controls.Add(newVenueName);
            newPanel.Controls.Add(newPictureBox);
            newPanel.Name = evt.Name + "Panel";
            newPanel.Width = layoutPanel.Width;
            newPanel.Height = 83;
            newPanel.TabIndex = 1;
            newPanel.BackColor = Color.Black;
            newPanel.ForeColor = Color.White;
            newPanel.BorderStyle = BorderStyle.Fixed3D;
            newPanel.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left)));

            this.layoutPanel.Controls.Add(newPanel);
            newPanel.BringToFront();
        }


        public void display_events(IList<Event> eventsList)
        {
            clearEvents();

            //add each new event
            foreach (Event newEvt in eventsList)
            {
                Add_Event(newEvt);
            }
        }

        public void clearEvents()
        {
            layoutPanel.Controls.Clear();
        }

        #endregion

        #region Event Handlers

        void newWebsiteLink_Click(object sender, EventArgs e)
        {
            string url = ((LinkLabel)sender).Text;
            if (url.StartsWith("www."))
            {
                url = "www." + url;
            }

            System.Diagnostics.Process.Start(url);
        }

        private void homeBtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.SelectMDIForm(MainForm.homeFrm);
        }

        private void layoutPanel_Resize(object sender, EventArgs e)
        {
            foreach (Panel p in layoutPanel.Controls)
            {
                p.Width = layoutPanel.Width;
            }
        }

        void VenueName_Click(object sender, EventArgs e)
        {
            //load the venue corresponding to the id in the tag, show that venue in the VenueMDI
            LinkLabel senderLink = (LinkLabel)sender;
            Location loadLoc = Globals.getLocationService().getById((int)senderLink.Tag);
            MainForm.venueFrm.load_venue(loadLoc);
            MainForm.SelectMDIForm(MainForm.venueFrm);
        }

        void EventName_Click(object sender, EventArgs e)
        {
            //load the venue corresponding to the id in the tag, show that venue in the VenueMDI
            LinkLabel senderLink = (LinkLabel)sender;
            Event loadEvent = Globals.getEventService().getById((int)senderLink.Tag);
            MainForm.eventFrm.load_event(loadEvent);
            MainForm.SelectMDIForm(MainForm.eventFrm);
        }


        private void homeBtn_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.SelectMDIForm(MainForm.homeFrm);
        }

        #endregion

    }
}